package com.mar.rover;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class NASASystem {
	private int platX;
	private int platY;
	
	private Map<Rover, String> rovers = new LinkedHashMap<Rover, String>();
	
	public String executeCommands(String input) {
		initPlat(input);
		initRover(input);
		Set<Rover> roverKeys = rovers.keySet();
		String result = "";
		for (Rover rover : roverKeys) {
			char[] commands = this.rovers.get(rover).toCharArray();
			for (int i = 0; i < commands.length; i++) {
				if('R' == commands[i])
				{
					rover.turnRight();
					System.out.println(rover.reportCurrentStatus());
				}
				if('L' == commands[i])
				{
					rover.turnLeft();
					System.out.println(rover.reportCurrentStatus());
				}
				if('M' == commands[i])
				{
					rover.move();
					System.out.println(rover.reportCurrentStatus());
				}
			}
			result += rover.reportCurrentStatus() + "\n";
		}
		return result.substring(0, result.length()-1);
	}

	public void initPlat(String input) {
		String planStr = input.split("\n")[0];
		platX = Integer.valueOf(planStr.split(" ")[0]);
		platY = Integer.valueOf(planStr.split(" ")[1]);
	}

	@Override
	public String toString() {
		return "" + platX + " " + platY;
	}

	public void initRover(String input) {
		String[] planStr = input.split("\n");
		for (int i = 1; i < planStr.length; i+=2) 
		{
			Rover rover = new Rover(Integer.valueOf(planStr[i].split(" ")[0]),
									Integer.valueOf(planStr[i].split(" ")[1]),
									planStr[i].split(" ")[2]);
			rovers.put(rover, planStr[i+1]);
		}
	}
	
	public Map<Rover, String> getRovers()
	{
		return this.rovers;
	}
}
